//
//  ZallCheckBoxCell.m
//  ZJS-DRIVER
//
//  Created by so on 16/6/3.
//  Copyright © 2016年 liulei. All rights reserved.
//

#import "ZallCheckBoxCell.h"

@implementation ZallCheckBoxCell
@synthesize checkBoxSize = _checkBoxSize;
@synthesize checkBox = _checkBox;

#pragma mark - life cycle
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if(self) {
        _checkBoxSize = CGSizeMake(30, 30);
    }
    return (self);
}
#pragma mark -

#pragma mark - override
- (void)layoutSubviews {
    [super layoutSubviews];
    
    CGRect insetFrame = UIEdgeInsetsInsetRect(self.contentView.bounds, _contentInsets);
    if(_checkBox) {
        insetFrame.size.width -= (_gap.width + _checkBoxSize.width);
    }
    
    if(_imageView) {
        if(_autoFitImageSize) {
            _imageSize = CGSizeMake(CGRectGetHeight(insetFrame), CGRectGetHeight(insetFrame));
        }
        self.imageView.frame = CGRectMake(_contentInsets.left, _contentInsets.top, _imageSize.width, _imageSize.height);
        insetFrame.origin = CGPointMake(self.imageView.right + _gap.width, self.imageView.top);
        insetFrame.size = CGSizeMake(CGRectGetWidth(insetFrame) - self.imageView.width - _gap.width, CGRectGetHeight(insetFrame));
    }
    if(_textLabel) {
        if(_autoFitTextHeight && _detailTextLabel) {
            _textHeight = self.textLabel.font.lineHeight;
        } else {
            _textHeight = CGRectGetHeight(insetFrame);
        }
        self.textLabel.frame = CGRectMake(CGRectGetMinX(insetFrame), CGRectGetMinY(insetFrame), CGRectGetWidth(insetFrame), _textHeight);
        insetFrame.origin = CGPointMake(self.textLabel.left, self.textLabel.bottom + _gap.height);
        insetFrame.size = CGSizeMake(self.textLabel.width, CGRectGetHeight(insetFrame) - self.textLabel.height - _gap.height);
    }
    if(_detailTextLabel) {
        _detailTextLabel.frame = insetFrame;
    }
    
    if(_checkBox) {
        CGRect inFrame = UIEdgeInsetsInsetRect(self.contentView.bounds, _contentInsets);
        self.checkBox.right = CGRectGetMaxX(inFrame);
        self.checkBox.centerY = CGRectGetMidY(inFrame);
    }
}

- (void)prepareForReuse {
    [super prepareForReuse];
    _checkBoxSize = CGSizeMake(30, 30);
    if(_checkBox) {
        _checkBox.enabled = YES;
        _checkBox.highlighted = NO;
        _checkBox.selected = NO;
    }
}
#pragma mark -

#pragma mark - setter
- (void)setCheckBoxSize:(CGSize)checkBoxSize {
    [self willChangeValueForKey:@"checkBoxSize"];
    _checkBoxSize = checkBoxSize;
    [self didChangeValueForKey:@"checkBoxSize"];
    [self setNeedsLayout];
}
#pragma mark -

#pragma mark - getter
- (ZallCheckBox *)checkBox {
    if(!_checkBox) {
        _checkBox = [[ZallCheckBox alloc] initWithFrame:CGRectMake(0, 0, _checkBoxSize.width, _checkBoxSize.height)];
        _checkBox.backgroundColor = [UIColor clearColor];
        _checkBox.contentInsets = UIEdgeInsetsZero;
        [self.contentView addSubview:_checkBox];
        [self setNeedsLayout];
    }
    return (_checkBox);
}
#pragma mark -

@end
